package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.R;
import com.hongxun.design.bean.FootInvolBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/6.
 */
public class InvolAndFinishAdapter extends BaseAdapter {
    Context context;
    List<FootInvolBean> beans;
    LayoutInflater inflater;
    List<SystemTypeBean> projectType;
    public InvolAndFinishAdapter(Context context, List<FootInvolBean> beans) {
        this.context = context;
        this.beans = beans;
        inflater = LayoutInflater.from(context);
        projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
    }


    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int i) {
        return beans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FootInvolBean bean = (FootInvolBean) getItem(i);
        Hollder hollder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_footinvol, null);
            hollder = new Hollder(view);
            view.setTag(hollder);
        } else {
            hollder = (Hollder) view.getTag();
        }

        try {
            hollder.time.setText(bean.create_time);
            if (bean.in_work!=null){
                hollder.name.setText(bean.p_name);
                hollder.image.setImageURI(UriUtil.getUri(bean.p_thumb));
                hollder.work.setVisibility(View.VISIBLE);
                if (bean.p_type!=null&&bean.p_type.length()>0){
                    hollder.type.setText(getProjectType(bean.p_type)+"项目");
                    hollder.type.setVisibility(View.VISIBLE);
                }else{
                    hollder.type.setVisibility(View.GONE);
                }

                hollder.work.setText("参与工作："+bean.in_work);

            }else{
                hollder.name.setText(bean.title);
                hollder.image.setImageURI(UriUtil.getUri(bean.article_thumb));
                hollder.type.setVisibility(View.GONE);
                hollder.work.setVisibility(View.GONE);
            }

        } catch (Exception e) {
        }
        return view;
    }


    class Hollder {

        TextView name, type, time, work;
        SimpleDraweeView image;

        Hollder(View view) {
            image = (SimpleDraweeView) view.findViewById(R.id.footinvol_image);
            name = (TextView) view.findViewById(R.id.footinvol_name);
            type = (TextView) view.findViewById(R.id.footinvol_type);
            time = (TextView) view.findViewById(R.id.footinvol_time);
            work = (TextView) view.findViewById(R.id.footinvol_work);
        }
    }

    private String getProjectType(String code) {
        if (projectType == null)
            projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        for (SystemTypeBean s : projectType) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }
}
